package com.iyao_app.http;


import android.text.TextUtils;

import com.iyao_app.entry.User;
import com.iyao_app.util.DbUtil;

import java.util.HashMap;
import java.util.List;

/**
 * 简介 ：
 * 作者 ： hjb
 * 时间 ：2017/11/8.
 */
public class NetUtils {
    private static volatile NetUtils single = null;


    public static NetUtils getInstance() {
        if (single == null) {
            synchronized (NetUtils.class) {
                if (single == null) {
                    single = new NetUtils();
                }
            }
        }
        return single;
    }

    private void norGet(String url, HashMap<String, Object> params, final NetCallBack callback, final Class classType) {
        HttpUtil.getIntance().get(url, params, callback, classType);
    }

    private void norPost(String url, HashMap<String, Object> params, final NetCallBack callback, final Class classType) {
        HttpUtil.getIntance().post(url, params, callback, classType);
    }


    public void uploadImage(String url, int width, int height, HashMap<String, Object> params, final NetCallBack callback, final Class classType) {
        HttpUtil.getIntance().upLoadFile(url + "?widthAndHeight=" + width + "*" + height, params, callback, classType);
    }

    public void login(String phone, String password, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("phone", phone);
        params.put("password", password);
        norPost(RequestUrl.LOGIN, params, callback, classType);
    }

    public void register(String phone, String password, String invitationCode, String code, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("phone", phone);
        params.put("password", password);
        params.put("invitationCode", invitationCode);
        params.put("source", 2);
        params.put("code", code);
        norPost(RequestUrl.REGISTER, params, callback, classType);
    }

    /**
     * 检查手机号与验证码
     *
     * @param phone
     * @param dynamicCode
     * @param callback
     * @param classType
     */
    public void check(String phone, String dynamicCode, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("phone", phone);
        params.put("dynamicCode", dynamicCode);
        norGet(RequestUrl.CHECK, params, callback, classType);
    }

    /**
     * 发送验证码
     *
     * @param phone
     * @param callback
     * @param classType
     */
    public void sms(String phone, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("phone", phone);
        norPost(RequestUrl.SMS, params, callback, classType);
    }

    /**
     * 完善用户信息
     *
     * @param userId
     * @param birthday
     * @param avatarUrl
     * @param nickname
     * @param sex
     * @param callback
     * @param classType
     */
    public void completeInfo(String userId, String birthday, String avatarUrl, String nickname, String sex,
                             final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("birthday", birthday);
        params.put("avatarUrl", avatarUrl);
        params.put("city", "");
        params.put("descSign", "");
        params.put("tags", "");
        params.put("vioceSignUrl", "");
        params.put("province", "");
        params.put("district", "");
        params.put("country", "");
        params.put("nickname", nickname);
        params.put("sex", sex);
        norPost(RequestUrl.COMPLETE_INFO, params, callback, classType);
    }

    /**
     * 修改密码
     *
     * @param userId
     * @param oldPassword
     * @param confirmPassword
     * @param newPassword
     * @param callback
     * @param classType
     */
    public void updatePassword(String userId, String oldPassword, String confirmPassword, String newPassword, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("oldPassword", oldPassword);
        params.put("confirmPassword", confirmPassword);
        params.put("newPassword", newPassword);
        norPost(RequestUrl.UPDATE_PASSWORD, params, callback, classType);
    }

    /**
     * 重置密码
     *
     * @param phone
     * @param code
     * @param newPassword
     * @param callback
     * @param classType
     */
    public void resetPassword(String phone, String code, String newPassword, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("phone", phone);
        params.put("code", code);
        params.put("newPassword", newPassword);
        norPost(RequestUrl.RESET_PASSWORD, params, callback, classType);
    }

    /**
     * 查询用户信息
     *
     * @param userId
     * @param callback
     * @param classType
     */
    public void userInfo(String userId, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        norGet(RequestUrl.USER_INFO + "/" + userId, params, callback, classType);
    }

    /**
     * 签到或者发表故事墙
     *
     * @param userId
     * @param callback
     * @param classType
     */
    public void event(String userId, String sceneId, double longitude, double latitude, String eventDesc,
                      int eventType, List<String> notifyUserIds, List<String> eventFileUrl, String sceneName, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("longitude", longitude);
        params.put("latitude", latitude);
        params.put("eventShowType", 1);
        params.put("eventDesc", eventDesc);
        params.put("sceneId", sceneId);
        params.put("eventType", eventType);
        params.put("notifyUserIds", notifyUserIds);
        params.put("eventFileUrl", eventFileUrl);
        params.put("sceneName", sceneName);
        norPost(RequestUrl.EVENT, params, callback, classType);
    }

    /**
     * 附近的人
     *
     * @param
     * @param callback
     * @param classType
     */
    public void nearByPerson(double longitude, double latitude, int radius, int pageSize, int page, int type, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        User user = DbUtil.getUser();
        if (user != null) {
            params.put("userId", user.id);
        }
        params.put("longitude", longitude);
        params.put("latitude", latitude);
        params.put("radius", radius);
        params.put("pageSize", pageSize);
        params.put("page", page);
        params.put("sexType", type);
        norPost(RequestUrl.NEAR_BY_PERSON, params, callback, classType);
    }

    /**
     * 附近最热的场景
     *
     * @param
     * @param callback
     * @param classType
     */
    public void round(double longitude, double latitude, int radius, int pageSize, int page, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();

        String location = longitude + "," + latitude;
        params.put("location", location);
        params.put("radius", radius);
        params.put("pageSize", pageSize);
        params.put("page", page);
        norPost(RequestUrl.ROUND, params, callback, classType);
    }

    /**
     * 最近常去场景
     *
     * @param
     * @param callback
     * @param classType
     */
    public void oftenArrive(double longitude, double latitude, int radius, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        User user = DbUtil.getUser();
        if (user != null) {
            params.put("userId", user.id);
//            LogUtil.e("===" + user.userId);
            params.put("longitude", longitude);
            params.put("latitude", latitude);
            params.put("radius", radius);
            norPost(RequestUrl.FREQUENTLY_PATRONIZE_SCENE, params, callback, classType);
        }
    }

    public void oftenArrive(String userId, double longitude, double latitude, int radius, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("userId", userId);
//            LogUtil.e("===" + user.userId);
        params.put("longitude", longitude);
        params.put("latitude", latitude);
        params.put("radius", radius);
        norPost(RequestUrl.FREQUENTLY_PATRONIZE_SCENE, params, callback, classType);
    }

    /**
     * 附近最热的场景
     *
     * @param
     * @param callback
     * @param classType
     */
    public void hotScene(double longitude, double latitude, int radius, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("longitude", longitude);
        params.put("latitude", latitude);
        params.put("radius", radius);
        norPost(RequestUrl.FAVORITE_SCENE, params, callback, classType);
    }

    /**
     * 获取用户关系列表
     *
     * @param
     * @param page
     * @param pageSize
     * @param type
     * @param callback
     * @param classType
     */

    public void getFriends(String userId, String nickName, String favicon, int sex, int page, int pageSize, int type, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();

        params.put("userId", userId);
        params.put("nickName", nickName);
        params.put("favicon", favicon);
        params.put("sex", sex);
        params.put("page", page);
        params.put("pageSize", pageSize);
        params.put("type", type);
        norPost(RequestUrl.ATTENTION, params, callback, classType);
    }

    /**
     * 发送消息  普通消息
     *
     * @param ope
     * @param to
     * @param type
     * @param body
     * @param callback
     * @param classType
     */

    public void sendmsg(int ope, String to, int type, String body, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        User user = DbUtil.getUser();
        if (user != null) {
            String from = user.id;
            params.put("from", from);
        }
        params.put("ope", ope);
        params.put("to", to);
        params.put("type", type);
        params.put("body", "{\"msg\":\"我是佟雪,赶快来IFC签到吧\"}");
        params.put("option", "{\"push\":true,\"needPushNick\":true}");
        params.put("pushcontent", "我是佟雪,赶快来IFC签到吧");
        norPost(RequestUrl.SENDMSG, params, callback, classType);
    }

    /**
     * 关注用户
     *
     * @param friendId
     * @param callback
     * @param classType
     */
    public void followFriend(String friendId, String nickName, String favicon, int sex, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        User user = DbUtil.getUser();
        if (user != null) {
            String userId = user.id;
            params.put("userId", userId);
        }
        params.put("nickName", nickName);
        params.put("favicon", favicon);
        params.put("sex", sex);
        params.put("friendId", friendId);
        norPost(RequestUrl.FOLLOWFRIEND, params, callback, classType);
    }

    /**
     * 取消关注用户
     *
     * @param friendId
     * @param callback
     * @param classType
     */
    public void unfollowFriend(String friendId, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        User user = DbUtil.getUser();
        if (user != null) {
            String userId = user.id;
            params.put("userId", userId);
        }
        params.put("friendId", friendId);
        norPost(RequestUrl.UNFOLLOWFRIEND, params, callback, classType);
    }

    /**
     * 关键字搜索
     *
     * @param page
     * @param pageSize
     * @param city
     * @param keywords
     * @param callback
     * @param classType
     */
    public void keyword(int page, int pageSize, String city, String keywords, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("page", page);
        params.put("pageSize", pageSize);
        params.put("city", city);
        params.put("keywords", keywords);
        norPost(RequestUrl.KEYWORD, params, callback, classType);
    }

    /**
     * 距离用户最近领主场景信息
     *
     * @param longitude
     * @param latitude
     * @param radius
     * @param callback
     * @param classType
     */
    public void manor(double longitude, double latitude, int radius, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        User user = DbUtil.getUser();
        if (user != null) {
            params.put("userId", user.id);
            params.put("longitude", longitude);
            params.put("latitude", latitude);
            params.put("radius", radius);
            norPost(RequestUrl.MANOR, params, callback, classType);
        }
    }

    public void sceneInfo(String sceneId, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("sceneId", sceneId);
        norPost(RequestUrl.SCENE_INFO, params, callback, classType);
    }


    /**
     * 查询用户信息
     *
     * @param userId
     * @param callback
     * @param classType
     */
    public void getOtherInfo(String userId, String otherId, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("visitorId", userId);
        params.put("userId", otherId);
        norGet(RequestUrl.USER_INFO, params, callback, classType);
    }

    /**
     * 获取在当前场景签到的好友
     *
     * @param userId
     * @param sceneId
     * @param callback
     * @param classType
     */
    public void currentSceneFriend(String userId, String sceneId, int page, int pageSize, int sex, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("sceneId", sceneId);
        params.put("sex", sex);
        params.put("page", page);
        params.put("pageSize", pageSize);
        norPost(RequestUrl.CURRENT_SCENE_FRIEND, params, callback, classType);
    }

    /**
     * 获取在当前场景签到的用户（好友除外）
     *
     * @param userId
     * @param sceneId
     * @param callback
     * @param classType
     */
    public void currentSceneRoundUser(String userId, String sceneId, int page, int pageSize, int sex, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("sceneId", sceneId);
        params.put("sex", sex);
        params.put("page", page);
        params.put("pageSize", pageSize);
        norPost(RequestUrl.CURRENT_SCENE_ROUND_USER, params, callback, classType);
    }

    /**
     * 打招呼接口
     *
     * @param sayHelloToUserId
     * @param sayHelloFromUserId
     * @param commentText
     * @param callback
     * @param classType
     */
    public void sayHello(String sayHelloToUserId, String sayHelloFromUserId, String commentText, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("sayHelloToUserId", sayHelloToUserId);
        params.put("sayHelloFromUserId", sayHelloFromUserId);
        params.put("commentText", commentText);
        norPost(RequestUrl.SAY_HELLO, params, callback, classType);
    }

    /**
     * 获取在当前场景签到的用户（好友除外）
     *
     * @param sceneId
     * @param callback
     * @param classType
     */
    public void currentSceneUser(String sceneId, int page, int pageSize, int sex, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("sceneId", sceneId);
        params.put("sex", sex);
        params.put("page", page);
        params.put("pageSize", pageSize);
        User user = DbUtil.getUser();
        if (user != null) {
            String loginUserId = user.id;
            params.put("userId", loginUserId);
        }
        norPost(RequestUrl.CURRENT_SCENE_USER, params, callback, classType);
    }

    /**
     * 打招呼列表
     *
     * @param userId
     * @param page
     * @param pageSize
     * @param callback
     * @param classType
     */
    public void sayHelloList(String userId, int page, int pageSize, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("page", page);
        params.put("pageSize", pageSize);
        norGet(RequestUrl.SAY_HELLO, params, callback, classType);
    }

    /**
     * 谁看过我
     *
     * @param userId
     * @param page
     * @param pageSize
     * @param callback
     * @param classType
     */
    public void getVisitor(String userId, int page, int pageSize, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("page", page);
        params.put("pageSize", pageSize);
        norGet(RequestUrl.GET_VISITOR, params, callback, classType);
    }

    /**
     * 保存视觉印象接口
     *
     * @param userId
     * @param fileUrls
     * @param callback
     * @param classType
     */
    public void visualImpressions(String userId, List<String> fileUrls, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("fileUrls", fileUrls);
        params.put("userId", userId);
        norPost(RequestUrl.VISUAL_IMPRESSIONS, params, callback, classType);
    }

    /**
     * 更新用户接口
     *
     * @param userId
     * @param avatarUrl
     * @param city
     * @param nickname
     * @param callback
     * @param classType
     */
    public void userUpdate(String userId, String avatarUrl, String city, String nickname, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("avatarUrl", avatarUrl);
        params.put("city", city);
        params.put("nickname", nickname);
        norPost(RequestUrl.USER_UPDATE, params, callback, classType);
    }

    /**
     * 更新生日
     *
     * @param userId
     * @param birthDay
     * @param callback
     * @param classType
     */
    public void userUpdateBirthDay(String userId, long birthDay, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("birthDay", birthDay);
        norPost(RequestUrl.USER_UPDATE, params, callback, classType);
    }

    /**
     * app反馈接口
     *
     * @param userId
     * @param feedbackUrl
     * @param description
     * @param callback
     * @param classType
     */

    public void userAppFeedback(String phoneOrEmail, String userId, String feedbackUrl, String description, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("phoneOrEmail", phoneOrEmail);
        params.put("feedbackUrl", feedbackUrl);
        params.put("description", description);
        norPost(RequestUrl.USER_APPFEEDBACK, params, callback, classType);
    }

    /**
     * 故事 签到 发现 列表
     *
     * @param userId
     * @param sceneId
     * @param eventType
     * @param callback
     * @param classType
     */
    public void getEventList(String userId, String sceneId, String eventType, int page, int pageSize, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        if (!TextUtils.isEmpty(userId)) {
            params.put("userId", userId);
        }
        if (!TextUtils.isEmpty(sceneId)) {
            params.put("sceneId", sceneId);
        }
        if (!TextUtils.isEmpty(eventType)) {
            params.put("eventType", eventType);
        }
        User user = DbUtil.getUser();
        if (user != null) {
            String loginUserId = user.id;
            params.put("loginUserId", loginUserId);
        }
        params.put("page", page);
        params.put("pageSize", pageSize);
        norGet(RequestUrl.GET_EVENT_LIST, params, callback, classType);
    }

    /**
     * 故事详情
     *
     * @param userId
     * @param eventId
     * @param callback
     * @param classType
     */
    public void getEventDetail(String userId, int eventId, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("loginUserId", userId);
        params.put("eventId", eventId);
        norGet(RequestUrl.EVENT, params, callback, classType);
    }

    /**
     * 评论接口
     *
     * @param userId
     * @param eventId
     * @param commentText
     * @param callback
     * @param classType
     */
    public void comment(String userId, int eventId, String commentText, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("commentText", commentText);
        params.put("eventId", eventId);
        norPost(RequestUrl.COMMENT, params, callback, classType);
    }

    /**
     * 评论列表
     *
     * @param eventId
     * @param callback
     * @param classType
     */
    public void commentList(int eventId, int page, int pageSize, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("eventId", eventId);
        params.put("page", page);
        params.put("pageSize", pageSize);
        norGet(RequestUrl.COMMENT, params, callback, classType);
    }

    /**
     * 喜欢接口 (huangxiaoxiao)
     *
     * @param eventId
     * @param userId
     * @param callback
     * @param classType
     */
    public void goodOrBad(int eventId, String userId, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("eventId", eventId);
        params.put("userId", userId);
        params.put("goodOrBad", 1);
        norPost(RequestUrl.GOOD_OR_BAD, params, callback, classType);
    }

    /**
     * 发现轮播图
     *
     * @param callback
     * @param classType
     */
    public void getEventFound(final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        norGet(RequestUrl.EVENT_FOUND, params, callback, classType);
    }

    /**
     * 关注好友的故事列表
     *
     * @param loginUserId
     * @param callback
     * @param classType
     */
    public void getEventGetFriendsEvent(String loginUserId, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("loginUserId", loginUserId);
        params.put("eventType", 2);
        norGet(RequestUrl.EVENT_GETFRIENDSEVENT, params, callback, classType);
    }

    /**
     * 更新密码
     *
     * @param userId
     * @param oldPassword
     * @param newPassword
     * @param callback
     * @param classType
     */
    public void getUserUpdatepassword(String userId, String oldPassword, String newPassword, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("oldPassword", oldPassword);
        params.put("newPassword", newPassword);
        norPost(RequestUrl.USER_UPDATEPASSWORD, params, callback, classType);
    }

    /**
     * 领地
     *
     * @param userId
     * @param callback
     * @param classType
     */
    public void manorList(String userId, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        norPost(RequestUrl.MANOR_LIST, params, callback, classType);
    }

    /**
     * 分享
     *
     * @param userId
     * @param shareType
     * @param eventId
     * @param sceneId
     * @param callback
     * @param classType
     */
    public void share(String userId, String shareType, String eventId, String sceneId, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("shareType", shareType);
        params.put("eventId", eventId);
        params.put("sceneId", sceneId);
        params.put("userId", userId);
        norPost(RequestUrl.SHARE, params, callback, classType);
    }

    /**
     * 分享回调
     *
     * @param shareId
     * @param callback
     * @param classType
     */
    public void shareUpdate(int shareId, final NetCallBack callback, final Class classType) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("shareId", shareId);
        norPost(RequestUrl.SHARE_UPDATE, params, callback, classType);
    }
}
